﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace anowak.BDD.Compiler
{
    public class FileReader
    {
        private String fPath;
        private String[] contentLines = null;

        public String[] Content
        {
            get { return contentLines; }
        }

        public FileReader() { }

        public FileReader(String filePath)
        {
            this.fPath = filePath;
        }

        public void read()
        {
            if (fPath == null)
                throw new System.IO.FileLoadException("Zmienna pliku nie została zainicjalizowana");
            if (!System.IO.File.Exists(fPath))
                throw new System.IO.FileLoadException("Plik nie istnieje");
            try
            {
                    contentLines = System.IO.File.ReadAllLines(fPath);
            }
            catch (Exception ex)
            {
                throw new System.IO.IOException("Nie udało się odczytać zawartości pliku",ex);
            }
        }

        public void removeNonLetterOrDigit()
        {
            String[] newLines = new String[contentLines.Length];
            for (int i = 0; i < contentLines.Length;i++ )
            {
                char[] arr = contentLines[i].ToCharArray();
                arr = Array.FindAll<char>(arr, (c => (char.IsLetterOrDigit(c) || char.IsWhiteSpace(c) || c == '-')));
                newLines[i] = new string(arr);
            }
            contentLines = newLines;
        }
        public String ContentString()
        {
            String output = "";
            foreach (String line in contentLines)
                output += line + "\r\n";
            return output;
        }
    }
}
